<!DOCTYPE html>
<html lang="zh_CN">
<head>
<#--引入外部文件-->
    <#include "../common/header.ftl">
</head>
<body>

<div class="container" style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
        <#--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题">
                </div>
                <button id="btn_query" class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a href="javascript:" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加攻略文章
                </a>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th></th>
                </tr>
            <#--使用Freemark的指令完成循环迭代-->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.title}</td>
                        <td><img src="${entity.coverUrl}" width="30px"/></td>
                        <td>${(entity.releaseTime?string('yyyy-MM-dd HH:mm:ss'))!}</td>
                        <td>${entity.sequence}</td>
                        <td>${entity.catalog.name}</td>
                        <td>
                            <#if entity.state== 0 >
                                草稿
                            <#elseif entity.state==1>
                               发布
                            </#if>
                        </td>
                        <td>
                            <a href="javascript:" class="btn btn-info btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>

            <#include "../common/page.ftl">
        </div>
    </div>
</div>

<#--模态框必须是body的子元素-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog ">
        <div class="modal-content">
        <#--模态框标题-->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">新增/编辑</h4>
            </div>
        <#--模态框正文-->
            <div class="modal-body">
            <#--填充编辑界面-->
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm"
                      enctype="multipart/form-data">
                    <input type="hidden" name="id"/>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="title" placeholder="请输入攻略标题"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">封面：</label>
                        <div class="col-lg-6">
                            <img id="coverImg" width="100%"> <!--用来显示-->
                            <input type="hidden" class="form-control" name="coverUrl"/><!--用来提交表单(回显)-->
                            <input type="file" class="form-control" name="file"/><!--用来提交表单(上传)-->
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">所属攻略：</label>
                        <div class="col-lg-6">
                            <select class="form-control" id="strategySelect">
                                <#list strategies as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">攻略类别：</label>
                        <div class="col-lg-6">
                            <select class="form-control" id="catalogSelect" name="catalog.id">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">排序：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="sequence" placeholder="排序"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">状态：</label>
                        <div class="col-lg-6">
                            <select name="state" class="form-control">
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                            </select>
                        </div>
                    </div>
                    //文本编辑框
                    <textarea name="strategyContent.content" id="editor1"></textarea>
                </form>

            </div>
        <#--模态框底部按钮-->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitDetailBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<!--引入ckeditor-->
<script src="/js/plugins/ckeditor/ckeditor.js"></script>
<script>
    $(function () {
        //定义变量表示大攻略id
        var strategyId;
        //定义变量表示攻略分类id
        var strategyCatalogId;

        //初始化ckeditor
        var ck = CKEDITOR.replace('editor1');

        //模态框的表单提交
        $(".submitDetailBtn").click(function () {
            //获取编辑器中的内容,设置到textarea里面
            var data = ck.getData();
            $("#editor1").html(data);
            //提交表单
            $("#editForm").submit();
        });

        //定义函数，发送ajax查询攻略分类
        function getStrategyCatalog(){
            $.get('/strategyCatalog/listByStrategyId.do', {strategyId: strategyId}, function (data) {
                var temp = "";
                $.each(data, function (index, ele) {
                    temp += '<option value="' + ele.id + '">' + ele.name + '</option>';
                });
                $("#catalogSelect").html(temp);
                //回显攻略分类下拉框
                $("#catalogSelect").val(strategyCatalogId);
            });
        }
        //触发按键，弹出模态框
        $(".inputBtn").click(function () {
            //默认大攻略id为1
            strategyId = 1;
            //默认大攻略分类id为1
            strategyCatalogId = 1;
            //清空模态框的数据
            $("#inputModal input").val("");
            $("#coverImg").attr('src', '');
            //清空文本编辑框内容
            // ck.setData("");
            //获取当前按钮中的自定义JSON对象
            var obj = $(this).data("json");
            //只有编辑按钮才做数据回显
            if (obj) {
                $("#inputModal input[name='id']").val(obj.id);
                $("#inputModal input[name='title']").val(obj.title);
                $("#inputModal select[name='state']").val(obj.state);
                $("#inputModal input[name='coverUrl']").val(obj.coverUrl);
                $("#inputModal input[name='sequence']").val(obj.sequence);
                $("#coverImg").attr('src', obj.coverUrl);
                //回显攻略下拉框
                strategyId = obj.strategyId;
                //回显分类下拉框
                strategyCatalogId = obj.catalogId;
                //回显攻略文章内容
                $.get('/strategyDetail/getContentById.do', {id: obj.id}, function (data) {
                    ck.setData(data.content);
                });
            }
            //显示大攻略下拉框
            $("#strategySelect").val(strategyId);
            //根据攻略id获取所有的攻略分类
            getStrategyCatalog();
            $("#inputModal").modal('show');
        });
        //攻略下拉框
        $("#strategySelect").change(function () {
            //获取当前选择的攻略的id
            strategyId = $(this).val();
            //根据攻略id获取所有的攻略分类
            getStrategyCatalog();
        });
    });
</script>

</body>
</html>